package de.se2.sharebox.frames;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;



public class EinladungVerschicken extends JFrame implements ListSelectionListener {

	DefaultListModel dlm = new DefaultListModel();
	JList emails;
	private static final String addEmailString = "Add Email";
	JLabel ueberschrift;
	JTextField jtfemail;
	JButton DeleteEmail;
	JButton Verschicken;


//	Konstruktor
	public EinladungVerschicken(){

		setTitle("Einladung verschicken"); // Fenstertitel festlegen
		setDefaultCloseOperation(EXIT_ON_CLOSE); // Aktion zum Schliessen des Fensters festlegen
		GridBagLayout gbl = new GridBagLayout(); // Layoutmanager erstellen
		GridBagConstraints gbc; // GBConstraints erstellen --> wird durch Hilfsmethode gef�llt.
		setLayout(gbl); // Layout dem Fenster hinzuf�gen

		ueberschrift = new JLabel ("Ordnername");
		gbc = makegbc (0,0,0,1); // Hilfsmethode nutzen
		gbc.fill = GridBagConstraints.NONE; // Ausbreitung des Elements bei Skalierung des Fensters
		gbl.setConstraints( ueberschrift, gbc ); // Die Constraints dem Layoutmanger mitteilen und speichern
		add(ueberschrift); // Element dem Fenster hinzuf�gen

		emails = new JList(dlm);
		dlm.add(0, "email1");
		dlm.add(1, "email2");
		dlm.add(2, "email3");
		dlm.add(3, "email4");
		dlm.add(4, "email5");
		emails.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		emails.setSelectedIndex(0);
		emails.addListSelectionListener(this);
		gbc = makegbc(0,1,2,12);
		gbc.fill = GridBagConstraints.BOTH;
		gbc.weightx = 300; // Wenn Platz verf�gbar ist (durch Skalierung) soll dieser auch genutzt werden
		gbc.weighty = 300; //  s.o.
		gbl.setConstraints(emails, gbc);
		add(emails);

		JButton AddEmail = new JButton("Add Email");
		gbc = makegbc(2,2,1,1);
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.CENTER;
		gbl.setConstraints(AddEmail, gbc);
		add(AddEmail);
		AddEmail.setActionCommand(addEmailString);
		AddEmailListener addEmailListener = new AddEmailListener(AddEmail);
		AddEmail.addActionListener(addEmailListener);
		AddEmail.setEnabled(false);

		jtfemail = new JTextField();
		gbc = makegbc(2,1,1,1);
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.CENTER;
		gbl.setConstraints(jtfemail, gbc);
		add(jtfemail);
		jtfemail.addActionListener(addEmailListener);
		jtfemail.getDocument().addDocumentListener(addEmailListener);
		String name = dlm.getElementAt(emails.getSelectedIndex()).toString();

		DeleteEmail = new JButton("Delete Email");
		gbc = makegbc(2,3,1,1);
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.CENTER;
		gbl.setConstraints(DeleteEmail, gbc);
		add(DeleteEmail);
		DeleteEmail.setEnabled(false);
		DeleteEmail.addActionListener(new DeleteEmailListener());

		Verschicken = new JButton("Send");
		gbc = makegbc(2,4,1,1);
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.CENTER;
		gbl.setConstraints(Verschicken, gbc);
		add(Verschicken);
		Verschicken.setEnabled(true);

		pack();
	}

	public GridBagConstraints makegbc(int x, int y, int width, int height){

		GridBagConstraints gbc = new GridBagConstraints(); // GBC-Objekt erstellen
		gbc.gridx = x; // x-position im gridlayoutraster
		gbc.gridy = y; // y-position
		gbc.gridwidth = width; // besetzte Breite des Elements
		gbc.gridheight = height; // besetzte Hoehe des Elements
		gbc.insets = new Insets(2,8,2,8); // Rand festlegen = Mindestabstand zu anderen Elementen

		return gbc; // Rueckgabe des gbc objekts
	}






	class DeleteEmailListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			//This method can be called only if
			//there's a valid selection
			int index = emails.getSelectedIndex();
			dlm.remove(index);

			int size = dlm.getSize();

			if (size == 0) { //Nobody's left, disable firing.
				DeleteEmail.setEnabled(false);

			} else { //Select an index.
				if (index == dlm.getSize()) {
					//removed item in last position
					index--;
				}

				emails.setSelectedIndex(index);
				emails.ensureIndexIsVisible(index);
			}
		}
	}

//	This listener is shared by the text field and the AddEmail button.
	class AddEmailListener implements ActionListener, DocumentListener {
		private boolean alreadyEnabled = false;
		private JButton button;

		public AddEmailListener(JButton button) {
			this.button = button;
		}

		public void actionPerformed(ActionEvent e) {
			String name = jtfemail.getText();

			//User didn't type in a unique name...
			if (name.equals("") || alreadyInList(name)) {
				Toolkit.getDefaultToolkit().beep();
				jtfemail.requestFocusInWindow();
				jtfemail.selectAll();
				return;
			}

			int index = emails.getSelectedIndex(); //get selected index
			if (index == -1) { //no selection, so insert at beginning
				index = 0;
			} else {           //add after the selected item
				index++;
			}

			dlm.addElement(jtfemail.getText());

			//Reset the text field.
			jtfemail.requestFocusInWindow();
			jtfemail.setText("");

			//Select the new item and make it visible.
			emails.setSelectedIndex(index);
			emails.ensureIndexIsVisible(index);
		}


		protected boolean alreadyInList(String name) {
			return dlm.contains(name);
		}

		//Required by DocumentListener.
		public void insertUpdate(DocumentEvent e) {
			enableButton();
		}

		//Required by DocumentListener.
		public void removeUpdate(DocumentEvent e) {
			handleEmptyTextField(e);
		}

		//Required by DocumentListener.
		public void changedUpdate(DocumentEvent e) {
			if (!handleEmptyTextField(e)) {
				enableButton();
			}
		}

		private void enableButton() {
			if (!alreadyEnabled) {
				button.setEnabled(true);
			}
		}

		private boolean handleEmptyTextField(DocumentEvent e) {
			if (e.getDocument().getLength() <= 0) {
				button.setEnabled(false);
				alreadyEnabled = false;
				return true;
			}
			return false;
		}
	}

	//	This method is required by ListSelectionListener.
	public void valueChanged(ListSelectionEvent e) {
		if (e.getValueIsAdjusting() == false) {

			if (emails.getSelectedIndex() == -1) {
				//No selection, disable fire button.
				DeleteEmail.setEnabled(false);

			} else {
				//Selection, enable the fire button.
				DeleteEmail.setEnabled(true);
			}
		}
	}






	public static void main(String [] args){
		EinladungVerschicken einladung = new EinladungVerschicken();
		einladung.setSize(300,300);
		einladung.setVisible(true);
	}

}
